package configruation

import (
	"gitee.com/shijun123456/kcgin"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"log"
	"sinery.net.goft/src/common"
	"time"
)

//v2版本
func (this *DBConfig) Gormv2() *gorm.DB {
	dsn := common.GetMysqlDSN()
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   kcgin.AppConfig.String("db.prefix"), //设置表名前缀 Artile kc_article
			SingularTable: true,                                //启用单数表名 article kc_article
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	DB, err := db.DB()
	if err != nil {
		log.Fatal(err)
	}
	maxid, _ := kcgin.AppConfig.Int("db.maxidle")
	maxopen, _ := kcgin.AppConfig.Int("db.maxconn")
	maxlifetimeInt, _ := kcgin.AppConfig.Int("db.maxlifetime")
	maxlifetimeIntToDuration := time.Duration(maxlifetimeInt)
	DB.SetMaxIdleConns(maxid)
	DB.SetMaxOpenConns(maxopen)
	DB.SetConnMaxLifetime(maxlifetimeIntToDuration)
	return db
}
